package gh.library.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.aplus.kira.kiralibrary.tools.DarkStatusTextColorUtil;
import com.aplus.kira.kiralibrary.tools.StatusBarUtil;
import com.licheedev.adaptscreen.AdaptScreenEx;

import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import butterknife.ButterKnife;
import gh.library.R;
import gh.library.baseapp.AppManager;
import gh.library.baseapp.GhApp;
import gh.library.utils.TUtil;

public abstract class BaseActivity<T extends BasePresenter, E extends BaseModel> extends Activity {
    protected T mPresenter;
    protected E mModel;
    private long exitTime = 0;
    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        onBeforeSetContentView();
        if (getLayoutId() != 0)
            setContentView(getLayoutId());
        //绑定初始化ButterKnife
        ButterKnife.bind(this);
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        initPresenter();
        initView(savedInstanceState);
    }

    //带状态栏的全屏--内容可伸展到状态栏
    private void setFullScreenWithStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置状态栏透明
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //  设置根布局的参数
            //将根rootView直接顶上去，和状态栏的顶部对齐。
            ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            ViewCompat.setFitsSystemWindows(rootView, false);
            rootView.setClipToPadding(true);
        }
    }

    //简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
    protected void initPresenter() {
        if (mPresenter != null) {
            mPresenter.inject(this, this, mModel);
        }
    }

    //初始化view
    protected abstract void initView(Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.onDestroy();


        AppManager.getAppManager().finishActivity(this);
    }

    /*********************
     * 子类实现
     *****************************/
    //获取布局文件
    public abstract int getLayoutId();

    /**
     * 设置layout前配置
     */
    @SuppressLint("NewApi")
    protected void onBeforeSetContentView() {

        // 把actvity放到application栈中管理
        AppManager.getAppManager().addActivity(this);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setScreen(GhApp.getScreenType());
        Window window = getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(ContextCompat.getColor(this, getStatusBarColor()));
        if (getFullScreenWithStatusBar()) {
            setFullScreenWithStatusBar();
//            StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
//            new DarkStatusTextColorUtil().setDarkStatusTextColor(this, true);
        }
    }

    public abstract int getStatusBarColor();

    public abstract boolean getFullScreenWithStatusBar();

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }


    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 如设置竖屏、全屏、横屏，调用此方法进行相关设置
     */
    protected void setScreen(int type) {
        switch (type) {
            case 1:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case 2:
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
                break;
            case 3:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
        }

    }

    /**
     * 点击退出
     */
    protected void backLong() {

        if (System.currentTimeMillis() - exitTime > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        return toModifyResource(resources);
    }

    /**
     * 修改Resources
     *
     * @param originalResources 没动过手脚的Resources
     * @return
     */
    protected Resources toModifyResource(Resources originalResources) {
        // 建议先在Application里面初始化  AdaptScreenEx.init(context);
        // 假如设计图短边为1080像素
        return AdaptScreenEx.adaptShorter(originalResources, GhApp.getDesignShortSize());
    }
}
